﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RRRSRoguelike;
using RRRSRoguelike.Enums;
using System.Drawing;
using RRRSRoguelikeData;
using RRRSRoguelike.Managers;

namespace RRRSRoguelike.Entities
{
    public class Tile
    {

        #region variables
        public bool Active;
        protected TileData TileData = new TileData();
        #endregion
    
        #region properties

        public Point Position
        {
            get { return new Point(TileData.X, TileData.Y); }
            set { TileData.X = value.X; TileData.Y = value.Y; }
        }

        public bool Walkable
        {
        	get {return TileData.Walkable;}
        	set { TileData.Walkable= value;}
        
        }
        
        public int X
        {
            get { return (TileData.X); }
            set { TileData.X = value; }
        }

        public int Y
        {
            get { return (TileData.Y); }
            set { TileData.Y = value; }
        }

        public ConsoleColor Color
        {
            get { return Constants.ColorsDictionary[TileData.ForeColor]; }
            set { TileData.ForeColor = Constants.ColorsDictionary.FirstOrDefault(x=> x.Value == value).Key; }
        }
        
        public ConsoleColor Background
        {
        	get { return Constants.ColorsDictionary[TileData.BackColor]; }
            set { TileData.BackColor = Constants.ColorsDictionary.FirstOrDefault(x=> x.Value == value).Key; }
        }

        public string ImageCharacter
        {
            get { return (TileData.ImageCharacter); }
            set { TileData.ImageCharacter = value; }
        }

        public string Description
        {
            get { return (TileData.Description); }
            set { TileData.Description = value; }
        }
        
        public string Name
        {
            get { return (TileData.Name); }
            set { TileData.Name = value; }
        }
        
        #endregion

        #region constructors
        public Tile():base()
        {
            Initialize(0, 0);
        }

        public Tile(int x, int y)
            : base()
        {
            Initialize(x, y);
        }

        public Tile(Point point)
            : base()
        {
            Initialize(point.X, point.Y);
        }
        
        public Tile(Point point, TileData tileData)
            : base()
        {
            
        	this.TileData = (TileData)tileData.Clone();
            this.X = point.X;
            this.Y = point.Y;
        }
        
        public Tile(string imageCharacter, ConsoleColor color, Point position)
        {
            ImageCharacter = imageCharacter;
            Color = color;
            Background = ConsoleColor.Black;
            Position = position;
            Active = true;
        }


        #endregion

        #region methods

        public void Initialize(int x, int y)
        {
            this.X = x;
            this.Y = y;
            ImageCharacter = "";
            Color = Constants.ColorsDictionary["White"];
            Description = "See Me!";
        }

        public void Move(Point point, Direction direction, int distance)
        {

            switch (direction)
            {
                case Direction.N:
                    TileData.Y -= distance;
                    break;
                case Direction.S:
                    TileData.Y += distance;
                    break;
                case Direction.E:
                    TileData.X += distance;
                    break;
                case Direction.W:
                    TileData.X -= distance;
                    break;
            }
        }

        public void Kill(Creature creature)
        {
            creature.Hits = 0;
        }

        public bool IsVisuallySimilar(Tile tile)
        {
            if (tile != null)
            {
                if (this.Color == tile.Color && this.ImageCharacter == tile.ImageCharacter)
                    return true;
            }
            else
            {
                return true;
            }
            return false;

        } 
        #endregion

    }
}
